--region LayerRecharge.lua
--Date
--此文件由[BabeLua]插件自动生成

--功能: 充值面板
--create by niyinguo
--Date: 2017/06/09

local MessageTip = require('app.views.UI.MessageTip')
local WaitProgress = require('app.views.Controls.WaitProgress')
local platformMethod = require('app.Platform.platformMethod')

local LayerRecharge = class("LayerRecharge", require("app.views.UI.PopUI"))

function LayerRecharge:ctor()
    LayerRecharge.super.ctor(self)
    self:BindNodes(self.WidgetNode,
    "Panel_1.Button_Close",
    "Panel_1.rechargeSelect",
    "Panel_1.recodeSelect",
    "Panel_1.goldSelect",
    "Panel_1.Button_Agent",
    "Panel_1.RechargePanel",
    "Panel_1.RechargePanel.ListView_Recharge",
    "Panel_1.RechargePanel.ListView_Recharge.RechargeItem_0",
    "Panel_1.RechargePanel.ListView_Recharge.RechargeItem_1",
    "Panel_1.RechargePanel.ListView_Recharge.RechargeItem_2",
    "Panel_1.RechargePanel.ListView_Recharge.RechargeItem_3",
    "Panel_1.RechargePanel.ListView_Recharge.RechargeItem_4",
    "Panel_1.RechargePanel.ListView_Recharge.RechargeItem_5",
    "Panel_1.RechargeGoldPanel",
    "Panel_1.RechargeGoldPanel.ListView_RechargeGold",
    "Panel_1.RechargeGoldPanel.ListView_RechargeGold.RechargeGoldItem_0",
    "Panel_1.RechargeGoldPanel.ListView_RechargeGold.RechargeGoldItem_1",
    "Panel_1.RechargeGoldPanel.ListView_RechargeGold.RechargeGoldItem_2",
    "Panel_1.RechargeGoldPanel.ListView_RechargeGold.RechargeGoldItem_3",
    "Panel_1.RechargeGoldPanel.ListView_RechargeGold.RechargeGoldItem_4",
    "Panel_1.RechargeGoldPanel.ListView_RechargeGold.RechargeGoldItem_5",
    "Panel_1.RecodePanel",
    "Panel_1.Text_QQ",
    "Panel_1.Text_WeChat",
    "Panel_1.RecodePanel.ListView_Recode"
    )

    self.curPanelIndex = 0
    self:initEvent()
    self:showPanelIndex(1)
    if SystemDataManager.SystemSet~=nil then
        self.Text_QQ:setString("QQ号:"..SystemDataManager.SystemSet.customerService)
        self.Text_WeChat:setString("微信号："..SystemDataManager.SystemSet.weChatNumber)
    end

    local value = {6,16,30,68,128,328}
    for i = 1, 6 do
        UIHelper.getNode(self["RechargeItem_"..(i-1)],{"Image_Diamond"}):loadTexture("recharge_image_diamond_"..(i-1)..".png",ccui.TextureResType.plistType)
        UIHelper.getNode(self["RechargeItem_"..(i-1)],{"Image_Diamond"}):ignoreContentAdaptWithSize(true)
        UIHelper.getNode(self["RechargeItem_"..(i-1)],{"Text_Diamond"}):setString(Tool.GetRounding(value[i]*100/PlayerManager.Player.ratio))
        if PlayerManager.Player.inviteCode~= nil and string.len(PlayerManager.Player.inviteCode)>1 then
             print("当前邀请码"..PlayerManager.Player.inviteCode)
             UIHelper.getNode(self["RechargeItem_"..(i-1)],{"bindmark"}):setVisible(true)
             UIHelper.getNode(self["RechargeItem_"..(i-1)],{"bindmark","Text"}):setString("赠送"..Tool.GetRounding(value[i]*100/PlayerManager.Player.ratio*0.2).."钻石")
--             UIHelper.getNode(self["RechargeGoldItem_"..(i-1)],{"bindmark"}):setVisible(true)
        else
            UIHelper.getNode(self["RechargeItem_"..(i-1)],{"bindmark"}):setVisible(false)
--            UIHelper.getNode(self["RechargeGoldItem_"..(i-1)],{"bindmark"}):setVisible(false)
        end
    end
    
end

function LayerRecharge:initEvent()
    UIHelper.BindClickByButton(self.Button_Close,function(sender,event)
        self:Close()
    end)
    UIHelper.BindClickByButton(self.Button_Agent,function(sender,event)
        MessageTip.Show("复制成功")
        platformMethod.copyString(SystemDataManager.SystemSet.weChatNumber.."")
    end)

     UIHelper.BindClickByButtons({self.RechargeItem_0,self.RechargeItem_1,self.RechargeItem_2,self.RechargeItem_3,self.RechargeItem_4,self.RechargeItem_5},function(sender,event)
         if sender == self.RechargeItem_0 then
            self:rechargeMoney(6,1)
         elseif sender == self.RechargeItem_1 then
            self:rechargeMoney(16,2)
         elseif sender == self.RechargeItem_2 then
            self:rechargeMoney(30,3)
         elseif sender == self.RechargeItem_3 then
            self:rechargeMoney(68,4)
         elseif sender == self.RechargeItem_4 then
            self:rechargeMoney(128,5)
         elseif sender == self.RechargeItem_5 then
            self:rechargeMoney(328,6)
         end
    end)
     UIHelper.BindClickByButtons({self.RechargeGoldItem_0,self.RechargeGoldItem_1,self.RechargeGoldItem_2,self.RechargeGoldItem_3,self.RechargeGoldItem_4,self.RechargeGoldItem_5},function(sender,event)
         if sender == self.RechargeGoldItem_0 then
            self:rechargeGold(6,1)
         elseif sender == self.RechargeGoldItem_1 then
            self:rechargeGold(18,2)
         elseif sender == self.RechargeGoldItem_2 then
            self:rechargeGold(30,3)
         elseif sender == self.RechargeGoldItem_3 then
            self:rechargeGold(68,4)
         elseif sender == self.RechargeGoldItem_4 then
            self:rechargeGold(128,5)
         elseif sender == self.RechargeGoldItem_5 then
            self:rechargeGold(228,6)
         end
    end)

    self.rechargeSelect:addEventListener(function(sender,event)
        self:showPanelIndex(1)
        self.rechargeSelect:setSelected(true)
        SoundHelper.playMusicSound(65,0,false)
    end)
    self.goldSelect:addEventListener(function(sender,event)
        self:showPanelIndex(3)
        self.goldSelect:setSelected(true)
        SoundHelper.playMusicSound(65,0,false)
    end)
    self.recodeSelect:addEventListener(function(sender,event)
        self:showPanelIndex(2)
        self.recodeSelect:setSelected(true)
        SoundHelper.playMusicSound(65,0,false)
    end)

    
end

function LayerRecharge:rechargeMoney(money,index)
    if PlayerManager.Player.inviteCode~= nil and string.len(PlayerManager.Player.inviteCode)>1 then
        ServiceMessageManager.SendRequstRecharge("buy diamond",money*100)
    else
        require("app.views.UI.LayerBindTip").new():Show():setRechargeMoney(money,index,1)
    end
end

function LayerRecharge:rechargeGold(money,index)
    ServiceMessageManager.SendRequstRecharge("buy gold",money*100)
end

function LayerRecharge:showPanelIndex(index)
    if self.curPanelIndex == index then
        return
    end
    self.rechargeSelect:setSelected(false)
    self.recodeSelect:setSelected(false)
    self.goldSelect:setSelected(false)
    self.RechargePanel:setVisible(false)
    self.RecodePanel:setVisible(false)
    self.RechargeGoldPanel:setVisible(false)
    if index == 1 then
       self.RechargePanel:setVisible(true)
       self.rechargeSelect:setSelected(true)
    elseif index ==2 then
       WaitProgress.Show()
       ServiceMessageManager.GetRechargeList()
       self.recodeSelect:setSelected(true)
    elseif index ==3 then
       self.RechargeGoldPanel:setVisible(true)
        self.goldSelect:setSelected(true)
    end

    self.curPanelIndex = index
end

function LayerRecharge:getWidget()
    return "PopUIs/LayerRecharge.csb"
end
function LayerRecharge:onEnter()
    self.super.onEnter(self)

    MessageHandle.addHandle(Enum.RECHARGE_CLIENT, function(msgid,data)
        self.ListView_Recode:removeAllItems()
        local msg = hall_pb.RechargeResponse()
        msg:ParseFromString(data)
        for i = 1, #msg.recharges do
            local cell = cc.CSLoader:createNode('PopUIs/RechargeRecodeCell.csb'):getChildByName("RecodeItem")
            local orderLabel = cell:getChildByName("order")
            local moneyLabel  = cell:getChildByName("money")
            local stateLabel = cell:getChildByName("state")
            local timeLabel = cell:getChildByName("time")

            local item = msg.recharges[i]

            orderLabel:setString(item.rechargeNo)
            moneyLabel:setString(item.money)
            if item.success == 0 then
                stateLabel:setString("支付中")
            elseif item.success == 1 then
                stateLabel:setString("支付成功")
            elseif item.success == 2 then
                stateLabel:setString("支付失败")
            end
            timeLabel:setString(item.dateTime)
            cell:removeFromParent()
            self.ListView_Recode:pushBackCustomItem(cell)
        end
--        for i = 1, 5 do
--            local cell = cc.CSLoader:createNode('PopUIs/RechargeRecodeCell.csb'):getChildByName("RecodeItem")
--            local orderLabel = cell:getChildByName("order")
--            local moneyLabel  = cell:getChildByName("money")
--            local stateLabel = cell:getChildByName("state")
--            local timeLabel = cell:getChildByName("time")

--            local item = msg.recharges[i]

--            orderLabel:setString(i)
--            moneyLabel:setString(99999)
--            stateLabel:setString("成功")
--            timeLabel:setString("2017/06/12 17:07")
--            cell:removeFromParent()
--            self.ListView_Recode:pushBackCustomItem(cell)
--        end
         WaitProgress.Close()
          self.RecodePanel:setVisible(true)
    end,self)
end

function LayerRecharge:onExit()
    self.super.onExit(self)
    MessageHandle.removeHandle(self)
end

return LayerRecharge

--endregion
